package com.example.testing;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.widget.Toast;

public class UserManagement {
	Context context;
    SharedPreferences sharedPreferences;
    Editor editor;
    Intent intent;
    
    String URL = "http://smoothalicious.nl/webservice/user/",
    		result = "result",
    		details = "details",
    		entryTest = "entryTest",
    		userID, firstname, lastname, email, password, className;
    
    ProgressDialog progressDialog;
    List<NameValuePair> params;
    JSONParser jsonParser;
    JSONObject jsonObject;
     
    public UserManagement(Context context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences("userData", 0);
        editor = sharedPreferences.edit();
    }
    
    public void addUser(String firstname, String lastname, String email, String password, String className) {
    	this.firstname = firstname;
    	this.lastname = lastname;
    	this.email = email;
    	this.password = password;
    	this.className = className;
    	
    	new addUser().execute();
    }
    
    class addUser extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage("Registeren...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

		@Override
		protected String doInBackground(String... args) {
			try {
				params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("firstname", firstname));
                params.add(new BasicNameValuePair("lastname", lastname));
                params.add(new BasicNameValuePair("email", email));
                params.add(new BasicNameValuePair("password", password));
                params.add(new BasicNameValuePair("className", className));

                URL += "add.php";
                jsonParser = new JSONParser();
                jsonObject = jsonParser.makeHttpRequest(URL, "POST", params);
                
                if(jsonObject.getInt(result) == 1) {
            		intent = new Intent(context, LoginUser.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
                
                return jsonObject.getString(details);
			} catch (Exception exception) {
                return exception.toString();
            }
		}
		
        protected void onPostExecute(String details) {
        	progressDialog.dismiss();
            
        	Toast.makeText(context, details, Toast.LENGTH_LONG).show();
        }
	}
    
    public void loginUser(String email, String password)
    {
    	this.email = email;
    	this.password = password;
    	
    	new loginUser().execute();
    }
    
    class loginUser extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage("Inloggen...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

		@Override
		protected String doInBackground(String... args) {
			try {
				params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("email", email));
                params.add(new BasicNameValuePair("password", password));

                URL += "login.php";
                jsonParser = new JSONParser();
                jsonObject = jsonParser.makeHttpRequest(URL, "POST", params);
                
                if(jsonObject.getInt(result) == 1) {
                	editor.putBoolean("isLoggedIn", true);
            		editor.putString("userID", jsonObject.getString("userID"));
                    editor.putString("firstname", jsonObject.getString("firstname"));
                    editor.putString("lastname", jsonObject.getString("lastname"));
                    editor.commit();
                    
                	if(jsonObject.getInt(entryTest) == 1) {
                		intent = new Intent(context, MainMenu.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                	} else {
                		/*
                		intent = new Intent(context, Exam.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                        */
                	}
                }
                
                return jsonObject.getString(details);
			} catch (Exception exception) {
                return exception.toString();
            }
		}
		
        protected void onPostExecute(String details) {
        	progressDialog.dismiss();
        	Toast.makeText(context, details, Toast.LENGTH_LONG).show();
        }
	}
    
    /*public void newIntent(Context context, Class<?> nextIntent) {
    	if(sharedPreferences.getBoolean("isLoggedIn", true)) {
            intent = new Intent(context, nextIntent);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
        	intent = new Intent(context, LoginUser.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }*/
    
    public void logoutUser() {
    	editor.clear();
        editor.commit();
         
        intent = new Intent(context, LoginUser.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}